//#include <graphics/d3d11/Mesh.h>
#include <graphics/d3d11/primitives/cube.h>
#include <maths/Vector3.h>
#include <maths/Vector2.h>
//#include <graphics/d3d11/Mesh.h>

namespace fort
{
	Cube::Cube()
	{

	}


	void Cube::Init(Platform &platform, float x, float y, float z, float scale)
	{

		const Mesh::vertex vertices[] = {
				{ Vector3(1, -1, -1), Vector3(0.577f, -0.577f, -0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(1, 1, -1), Vector3(0.577f, 0.577f, -0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(-1, 1, -1), Vector3(-0.577f, 0.577f, -0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(-1, -1, -1), Vector3(-0.577f, -0.577f, -0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(1, -1, 1), Vector3(0.577f, -0.577f, 0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(1, 1, 1), Vector3(0.577f, 0.577f, 0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(-1, 1, 1), Vector3(-0.577f, 0.577f, 0.577f), Vector2(0.0f, 0.0f) },
				{ Vector3(-1, -1, 1), Vector3(-0.577f, -0.577f, 0.577f), Vector2(0.0f, 0.0f) } };

		const UInt32 indices[] = {
			// Back
			0, 1, 2,
			2, 3, 0,
			// Front
			6, 5, 4,
			4, 7, 6,
			// Left
			2, 7, 3,
			2, 6, 7,
			// Right
			0, 4, 1,
			5, 1, 4,
			// Top
			6, 2, 1,
			5, 6, 1,

			// Bottom
			0, 3, 7,
			0, 7, 4
		};

		CreateBuffers(platform, vertices, indices);
	}


	void Cube::CreateBuffers(Platform &platform, const vertex verts[], const UInt32 indices[])
	{
		const D3D11& platform_d3d = static_cast<const D3D11&>(platform);
		//vertices = verts;

		// Vertex buffer
		D3D11_BUFFER_DESC desc;
		ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
		desc.ByteWidth = sizeof(verts);
		desc.Usage = D3D11_USAGE_DEFAULT;
		desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;

		D3D11_SUBRESOURCE_DATA data;
		ZeroMemory(&data, sizeof(D3D11_SUBRESOURCE_DATA));
		data.pSysMem = &verts;

		/*HR(*/platform_d3d.Device()->CreateBuffer(&desc, &data, &vertex_buffer)/*)*/;

		//-- set the indices here

		D3D11_BUFFER_DESC ibd;
		ibd.Usage = D3D11_USAGE_IMMUTABLE;
		ibd.ByteWidth = sizeof(indices);
		ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
		ibd.CPUAccessFlags = 0;
		ibd.MiscFlags = 0;
		D3D11_SUBRESOURCE_DATA iinitData;
		iinitData.pSysMem = &indices[0]; 
		/*HR(*/platform_d3d.Device()->CreateBuffer(&ibd, &iinitData, &index_buffer/*)*/);
	}

	void Cube::Render()
	{
		
		/*UINT stride = 12;
		UINT offset = 0;
		//-- Set the created buffers
		md3dDeviceContext->IASetInputLayout(InputLayouts::PosNormalTexTan);
		md3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
		md3dDeviceContext->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset);
		md3dDeviceContext->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R32_UINT, 0);


		//-- Draw the cube
		md3dDeviceContext->DrawIndexed(36, 0, 0);*/


	}
	Cube::~Cube()
	{
		//delete index_buffer;
		//delete vertex_buffer;
	}
}